package com.lancoo.ccas53.schedules;

import com.lancoo.ccas53.entity.CssTaskRound;
import com.lancoo.ccas53.service.CssTaskRoundService;
import com.lancoo.ccas53.service.impl.YunBasicServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @Author: mabolin
 * @Date: 2024-10-21-8:55
 * @Description:
 */
@Component
@Slf4j
@EnableAsync(proxyTargetClass = true)
public class SendMsgSchedule implements Serializable {

    @Resource
    private CssTaskRoundService cssTaskRoundService;
    @Resource
    @Lazy
    private YunBasicServiceImpl yunBasicService;

    /**
     * 选课结束提醒
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void sendSelectCourseEndMsg() {
        cssTaskRoundService.lambdaQuery()
                .ge(CssTaskRound::getEndTime, LocalDateTime.now().withHour(0).withMinute(0).withSecond(0))
                .le(CssTaskRound::getEndTime, LocalDateTime.now().withHour(23).withMinute(59).withSecond(59))
                .eq(CssTaskRound::getIsEnable,1)
                .list()
                .forEach(cssTaskRound -> {
                    Instant instant = cssTaskRound.getEndTime().toInstant();
                    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
                    if (cssTaskRound.getType() == 1) {
                        yunBasicService.sendSelectCourseEndMsg(localDateTime, cssTaskRound.getTaskId(), "预选", null);
                    } else if (cssTaskRound.getType() == 2) {
                        yunBasicService.sendSelectCourseEndMsg(localDateTime, cssTaskRound.getTaskId(), "正选", cssTaskRound.getId());
                    }
                });
    }

    /**
     * 选课结束提醒
     */
    @Scheduled(cron = "0 * * * * ?")
    public void sendSelectCourseEndedMsg() {
        log.info("========选课结束定时任务执行开始========");
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.plusMinutes(-1);
        List<CssTaskRound> list = cssTaskRoundService.lambdaQuery()
                .gt(CssTaskRound::getEndTime, start)
                .le(CssTaskRound::getEndTime, end)
                .eq(CssTaskRound::getIsEnable,1)
                .list();
        list
                .forEach(cssTaskRound -> {
                    yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getTaskId(),cssTaskRound.getRoundName(),cssTaskRound.getCreatedUserId(),cssTaskRound.getCreatedUserName());
//                    yunBasicService.sendSelectCourseEndedMsg(localDateTime, cssTaskRound.getTaskId(), cssTaskRound.getRoundName(), cssTaskRound.getId());
                });

        log.info("========选课结束定时任务执行结束========");
    }
}
